package com.example.controller;

import com.example.result.Result;
import com.example.service.MsmService;
import com.example.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/msm")
public class MsmApiController {

    @Autowired
    private MsmService msmService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //发送手机验证码
    @GetMapping("send/{phone}")
    public Result sendCode(@PathVariable String phone){
        //从Redis里面获取验证码 如果获取到 返回OK
        //key 手机号 value 验证码
        String code = redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(code)){
            return Result.ok();
        }
        //如果获取不到生成验证码
        //生成验证码，通过短信服务进行发送
        code= RandomUtil.getSixBitRandom();
        //通过阿里云短信服务进行发送
        boolean isSend=msmService.send(phone,code);
        System.out.println("isSend = " + isSend);
        //生成的验证码放到Redis 设置有效时间
        if (isSend){
            redisTemplate.opsForValue().set(phone,code,2, TimeUnit.MINUTES);
            return Result.ok();
        }
        return Result.fail().message("发送短信失败");
    }

}
